﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using Models;
using System.Data.Linq;
using System.Linq.Expressions;

namespace BAL
{
    public class CognitiveLevel : BaseBAL<CognitiveLevelInfo, Models.CognitiveLevel>
    {
        private static string CacheName = "AllCognitiveLevel";
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <returns></returns>
        public static void ToCache()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                CacheHelper.Refurbish(CacheName, db.CognitiveLevels.ToList(), 0, 60);
            }
        }

        /// <summary>
        /// 取得一条CognitiveLevelInfo
        /// </summary>
        /// <param name="InputId">信息ID</param>
        /// <returns></returns>
        public static CognitiveLevelInfo GetSingleInfo(int InputId)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.CognitiveLevel> CognitiveLevel_Info = (IList<Models.CognitiveLevel>)CacheHelper.GetCache(CacheName);
            return TransT2ToT1(CognitiveLevel_Info.Single(t => t.Id == InputId));
        }

        /// <summary>
        /// 取得全部列表
        /// </summary>
        /// <returns></returns>
        public static IList<CognitiveLevelInfo> GetList(int parentId)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.CognitiveLevel> CognitiveLevel_Info = (IList<Models.CognitiveLevel>)CacheHelper.GetCache(CacheName);
            return CognitiveLevel_Info.Where(w => w.ParentId == parentId).Select(t => TransT2ToT1(t)).ToList();
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="InstInfo">如果id=0则新增>0时编辑</param>
        /// <returns></returns>
        public static CognitiveLevelInfo Save(CognitiveLevelInfo InstInfo)
        {
            CognitiveLevelInfo CognitiveLevel_Info = InstInfo;
            if (InstInfo.Id > 0)
            {
                Edit(InstInfo);
            }
            else
            {
                CognitiveLevel_Info = Add(InstInfo);
            }
            ToCache();
            return CognitiveLevel_Info;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">id</param>
        public static void DelInfo(int id)
        {
            Question.DelQuestionByCognitiveLevel(id);
            Delete(id);
            ToCache();
        }

        /// <summary>
        /// 根据关键字检查数据是否已存在
        /// </summary>
        /// <param name="keyword">关键字</param>
        /// <param name="editid">自己的ID</param>
        /// <returns></returns>
        public static bool CheckCognitiveLevelExistByKeyword(string keyword, int editid)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.CognitiveLevel> CognitiveLevel_Info = (IList<Models.CognitiveLevel>)CacheHelper.GetCache(CacheName);
            return CognitiveLevel_Info.Where(t => t.Keyword == keyword && t.Id != editid).Count() > 0;
        }

        /// <summary>
        /// 根据名字检查数据是否已存在
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="keyword">关键字</param>
        /// <returns></returns>
        public static bool CheckCognitiveLevelExistByName(string name, string keyword)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.CognitiveLevel> CognitiveLevel_Info = (IList<Models.CognitiveLevel>)CacheHelper.GetCache(CacheName);
            return CognitiveLevel_Info.Where(t => t.Name == name || t.Keyword == keyword).Count() > 0;
        }

        /// <summary>
        /// 根据关键字取得ID
        /// </summary>
        /// <param name="keyword">关键字</param>
        /// <returns></returns>
        public static int GetCognitiveLevelIDByKeyword(string keyword)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.CognitiveLevel> CognitiveLevel_Info = (IList<Models.CognitiveLevel>)CacheHelper.GetCache(CacheName);
            return CognitiveLevel_Info.Single(t => t.Keyword == keyword).Id;
        }

        public static int GetFirstId()
        {
            if (!CacheHelper.IsExist(CacheName))
            {
                ToCache();
            }
            IList<Models.CognitiveLevel> cache = (IList<Models.CognitiveLevel>)CacheHelper.GetCache(CacheName);
            return cache.First<Models.CognitiveLevel>().Id;
        }


    }
}
